#include "stdafx.h"
#include "XIconDrawImpl.h"

#include "Util.h"
#include "DrawUtil.h"
#include "XCodeDefine.h"
#include "IXIconMgr.h"

IMPLEMENT_SKIN_CLASS(CXIconDrawImpl);

CXIconDrawImpl::CXIconDrawImpl()
{
    m_xIcon = NULL;

    memset(&m_Size, 0, sizeof(m_Size));
}

CXIconDrawImpl::~CXIconDrawImpl()
{
    ;
}


//////////////////////////////////////////////////////////////////////////
// IXIconDraw

HRESULT CXIconDrawImpl::put_IconPath(IN LPCTSTR szXNC, IN BOOL bBigIcon)
{
    ReleaseData();

    HRESULT hResult = S_OK;
    XPtr<IXIconMgr> pIconMgr = NULL;

    hResult = Util::CreateObject(&pIconMgr);
    CHECK_HRESULT(hResult);

    hResult = pIconMgr->LoadIcon(szXNC, bBigIcon, &m_xIcon);
    CHECK_HRESULT(hResult);

    if(DrawUtil::GetXIconSize(m_xIcon, &m_Size))
        return S_OK;
    return E_FAIL;
}

HRESULT CXIconDrawImpl::get_IconSize(OUT LPSIZE lpSize)
{
    memcpy(lpSize, &m_Size, sizeof(m_Size));
    return S_OK;
}

BOOL CXIconDrawImpl::IsDrawValid()
{
    SIZE size = {0};
    return (m_xIcon != NULL) && DrawUtil::GetXIconSize(m_xIcon, &size);
}

HRESULT CXIconDrawImpl::Draw(IN XCANVAS xCanvas, const RECT* lpDes)
{
    if(m_xIcon == NULL)
        return E_FAIL;

    HRESULT hResult = DrawUtil::DrawIcon(xCanvas, m_xIcon, lpDes);

    XASSERT(ASSERT_LEVEL_ERROR, SUCCEEDED(hResult), _T("CXIconDrawImpl::Draw"));

    return hResult;
}

HRESULT CXIconDrawImpl::Draw(IN XCANVAS xCanvas, IN const RECT* lpSrc, IN const RECT* lpDes)
{
    XASSERT_WARNING(FALSE, _T("IXIconDraw::Draw(xCanvas, lpSrc, lpDes) Not Implemented"));
    return E_NOTIMPL;
}

void CXIconDrawImpl::EnableColorize(IN BOOL bEnable)
{
    XASSERT_ERROR(FALSE, _T("IXIconDraw::EnableColorize(bEnable) Not Implemented"));
}

void CXIconDrawImpl::Colorize(IN int hue, IN int sat, IN int light)
{
    XASSERT_ERROR(FALSE, _T("IXIconDraw::Colorize(hue, sat, light) Not Implemented"));
}

//////////////////////////////////////////////////////////////////////////
// IBaseObj
void CXIconDrawImpl::OnFinalRelease()
{
    superclass::OnFinalRelease();
    ReleaseData();
}

//////////////////////////////////////////////////////////////////////////
// CXIconDrawImpl
void CXIconDrawImpl::ReleaseData()
{
    if(m_xIcon == NULL)
        return;

    HRESULT hResult = S_OK;
    XPtr<IXIconMgr> pIconMgr = NULL;

    hResult = Util::CreateObject(&pIconMgr);
    if(FAILED(hResult))
        return;

    pIconMgr->ReleaseIcon(m_xIcon);

    m_xIcon = NULL;
}
